import com.sun.source.tree.Tree;

/*
 * @author zzr
 * @date: 2025/10/17  19:41
 * @description: 判断一颗二叉树是否为对称二叉树
 */
public class Demo24 {


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;

        return isSymmetricChild(root.left, root.right);
    }

    private boolean isSymmetricChild(TreeNode left, TreeNode right) {
        if ((left == null && right != null) || (left != null && right == null)) {
            return false;
        }
        if (left == null && right == null ){
            return true;
        }
        if (left.val != right.val) {
            return false;
        }
        return isSymmetricChild(left.left, right.right) && isSymmetricChild(left.right, right.left);
    }
}
